package com.byt.framlib.commonutils;

import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tom on 2017/12/29.
 */
public class MoneyEditLishLister implements TextWatcher {

    private String filterString = "";
    private EditText mEditText;
    private TextView mButton;
    private Pattern pattern;//其它金额限制是999.99
    public ChangeListener mChangeListener;

    public MoneyEditLishLister(EditText mEditText, TextView button) {
        super();
        this.mEditText = mEditText;
        this.mButton = button;
        pattern = Pattern.compile("^0$|^\\d{0,6}$|(^\\d{0,6}\\.\\d{0,2}$)");
    }

    public MoneyEditLishLister(EditText mEditText, ChangeListener mChangeListener) {
        super();
        this.mEditText = mEditText;
        this.mChangeListener = mChangeListener;
        pattern = Pattern.compile("^0$|^\\d{0,6}$|(^\\d{0,6}\\.\\d{0,2}$)");
    }

    public MoneyEditLishLister(EditText mEditText, TextView button, ChangeListener mChangeListener) {
        super();
        this.mEditText = mEditText;
        this.mButton = button;
        this.mChangeListener = mChangeListener;
        pattern = Pattern.compile("^0$|^\\d{0,6}$|(^\\d{0,6}\\.\\d{0,2}$)");
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int selPos = start;
        Editable editable = mEditText.getText();
        String content = editable.toString();
        if (filterString.length() > content.length()) {
            selPos++;
        }
        if (filterString.equals(content)) {
            afterTextChanged(editable);
            return;
        }
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            if (content.contains(".")) {
                String preDot = content.substring(0, content.indexOf("."));
                if (preDot.length() > 1 && preDot.startsWith("0")) {
                    content = content.substring(1, content.length());
                    selPos = 1;
                } else if (TextUtils.isEmpty(preDot)) {
                    content = "0" + content;
                    selPos = 1;
                }

            } else {
                if (content.length() > 1 && content.startsWith("0")) {
                    content = content.substring(1, content.length());
                    selPos = 1;
                }

            }
        } else if (TextUtils.isEmpty(content)) {
            content = "";
            selPos = 0;
        } else {
            content = filterString;
        }

        if (mButton != null) {
            if (!TextUtils.isEmpty(content) && !content.endsWith(".") && Double.parseDouble(content) >= 0) {
                mButton.setEnabled(true);
            } else if (!TextUtils.isEmpty(content) &&  !content.endsWith(".")&& Double.parseDouble(content) <= 100){
                mButton.setEnabled(false);
            }
        }

        if (filterString.length() < content.length()) {
            selPos++;
        } else if (filterString.length() > content.length()) {
            selPos--;
        }
        if (selPos < 0) {
            selPos = 0;
        } else if (selPos > content.length()) {
            selPos = content.length();
        }
        mEditText.setText(content);
        editable = mEditText.getText();
        Selection.setSelection(editable, selPos);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        filterString = s.toString();
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (mChangeListener != null) {
            mChangeListener.afterTextChanged(mEditText.getText());
        }
    }

    public interface ChangeListener {
        void afterTextChanged(Editable s);
    }
}
